#pragma once

#include "../can_adapter.h"
#include "../can_transreceiver.h"
#include "mc1_cr.h"
#include "mc1_fmr.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/EventLoopThread.h"
#include <atomic>
#include <unordered_map>
namespace sensor::radar {
class CanTransreceiver;
class MankuCanAdapter : public CanAdapter {
public:
    explicit MankuCanAdapter(std::unordered_map<std::string, std::shared_ptr<CanTransreceiver>> devs);

    virtual void onReceiveCanMessage(const std::string& dev, int fid, muduo::net::Buffer& buffer) override;
    virtual void onReceiveGnssMessage(const interface::msg::Gnss::SharedPtr msg) override;
    virtual void onReceiveInformationMessage(const interface::msg::CarInformation msg) override;
    double degreesToRadians(double degrees);
    std::string printObject(const interface::msg::Object& object);

private:
    void onSendRadar2Handler();
    void onsendRadar3Handler();

    void onReceiverRS6bbFRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void fieldParsingfr01(interface::msg::Object& object, mc1_cr_rs_obj_fd_fr_t& objdf);
    void fieldParsingfr02(interface::msg::Object& object, mc1_cr_rs_obj_fd_fr_t& objdf);
    void onReceiverRS6abRRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void fieldParsingrr01(interface::msg::Object& object, mc1_cr_rs_obj_fd_rr_t& objdf);
    void fieldParsingrr02(interface::msg::Object& object, mc1_cr_rs_obj_fd_rr_t& objdf);

    void onReceiverRS6ebFInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void fieldParsingf01(interface::msg::Object& object, mc1_fmr_rs_obj_fd_t& objdf);
    void fieldParsingf02(interface::msg::Object& object, mc1_fmr_rs_obj_fd_t& objdf);

    void onReceiverRS520FRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiverRS510RRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);

    void onReceiverRS551FMRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiverRS511RRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);
    void onReceiverRS521FRInfo(const std::string& dev, int fid, muduo::net::Buffer& buffer);

private:
    using CanMessageUnpackFunction = std::function<void(const std::string& dev, int fid, muduo::net::Buffer& buffer)>;
    std::map<int, CanMessageUnpackFunction> message_unpack_table_;
    static std::unordered_map<int, int> MankuObjectTypeMap;

    std::mutex radar0MapLock_;
    std::mutex radar2MapLock_;
    std::mutex radar3MapLock_;

    std::map<int, std::pair<u_int64_t, interface::msg::Object>> radar0ObjectsMap;
    std::map<int, std::pair<u_int64_t, interface::msg::Object>> radar2ObjectsMap;
    std::map<int, std::pair<u_int64_t, interface::msg::Object>> radar3ObjectsMap;

    // std::shared_ptr<muduo::net::EventLoopThread> radar0LoopThread_ { nullptr };
    // std::shared_ptr<muduo::net::EventLoopThread> radar2LoopThread_ { nullptr };
    // std::shared_ptr<muduo::net::EventLoopThread> radar3LoopThread_ { nullptr };
};

}